package com.oa.admin.controller;

import com.joneying.common.web.response.Resp;
import com.oa.admin.entity.Inventory;
import com.oa.admin.service.IInventoryService;
import com.oa.core.PageResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.joneying.common.annotation.CommonController;
import org.springframework.context.annotation.Description;

import java.util.List;

import org.springframework.web.bind.annotation.RestController;
import com.oa.core.BaseController;

/**
 * @author syb123
 * @since 2019-01-22
 */
@RestController
@RequestMapping("/inventory")
@Api(description = "库存表模块")
public class InventoryController extends BaseController {

    private final IInventoryService inventoryService;

    @Autowired
    public InventoryController(IInventoryService inventoryService) {
        this.inventoryService = inventoryService;
    }

    @GetMapping("/list")
    @CommonController
    @Description("列表")
    @ApiOperation(value = "列表")
    public PageResult<Inventory> findInventoryList(
            @ApiParam("页号") @RequestParam(value = "pageNum", required = false) Integer pageNum,
            @ApiParam("行数") @RequestParam(value = "pageSize", required = false) Integer pageSize,
            @ApiParam("名称") @RequestParam(value = "itemName", required = false) String itemName,
            @ApiParam("预警状态 1-预警 2-未预警") @RequestParam(value = "warningStatus", required = false) String warningStatus
    ){
        return inventoryService.findInventoryList(pageNum, pageSize, itemName, warningStatus);
    }

    @GetMapping("/export")
    @CommonController
    @Description("列表")
    @ApiOperation(value = "列表")
    public void export(){
        inventoryService.export(response);
    }

    /**
     * 根据Id查询
     *
     * @param id id
     * @return Resp
     */
    @GetMapping("/{id}")
    @CommonController
    @Description("获取详细信息")
    @ApiOperation(value = "根据id查询")
    public Resp<Inventory> findById(@ApiParam(value = "id", required = true) @PathVariable String id) {
        return new Resp().success(inventoryService.findInventoryById(id));
    }


    /**
     * 新增
     *
     * @param inventory Inventory
     * @return Resp
     */
    @PostMapping("/add")
    @CommonController
    @Description("新增")
    @ApiOperation(value = "新增")
    public Resp add(@ApiParam(value = "库存表对象") @RequestBody(required = false) Inventory inventory) {
        if (inventoryService.insert(inventory)) {
            return new Resp().success();
        } else {
            return new Resp().failure();
        }
    }

    /**
     * 根据Id修改
     *
     * @param inventory Inventory
     * @return Resp
     */
    @PutMapping("/update")
    @CommonController
    @Description("根据id修改")
    @ApiOperation(value = "根据id修改")
    public Resp update(@ApiParam(value = "库存表对象") @RequestBody(required = false) Inventory inventory) {
        if (inventoryService.updateById(inventory)) {
            return new Resp().success();
        } else {
            return new Resp().failure();
        }
    }

    /**
     * 根据Id删除
     *
     * @param id id
     * @return Resp
     */
    @DeleteMapping("/{id}")
    @CommonController
    @Description("根据id删除")
    @ApiOperation(value = "根据id删除")
    public Resp delete(@ApiParam(value = "id", required = true) @PathVariable("id") String id) {
        Inventory inventory = inventoryService.selectById(id);
        if (inventory != null && inventory.getIsDeleted() == 2) {
            inventory.setIsDeleted(1);
            if (inventoryService.updateById(inventory)) {
                return new Resp().success();
            }
            return new Resp().failure();
        }
        return new Resp().failure();
    }
}
